package swu.cx.viewPagerGallery

import android.app.Application
import android.util.Log
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.android.volley.Request
import com.android.volley.toolbox.StringRequest
import com.google.gson.Gson
import kotlin.math.ceil
const val DATA_STATUS_CAN_LOAD_MORE = 0
const val DATA_STATUS_NO_MORE = 1
const val DATA_STATUS_NETWORK_ERROR = 2
class GalleryViewModel(application: Application) :AndroidViewModel(application) {
    private val _photoListLive = MutableLiveData<List<PhotoItem>>()
    private val _dataStatusLive = MutableLiveData<Int>()
    val photoListLive : LiveData<List<PhotoItem>>
        get() = _photoListLive
    val dataStatusLive:LiveData<Int>
        get() = _dataStatusLive

    private val perPage = 100
    private val keywords = arrayOf("cat","dog","car","beauty","phone","computer","flower","animal")
    private var currentPage = 1
    private var totalPage = 1
    private var currentKey = "cat"
    private var isNewQuery = true
    private var isLoading = false
    var needToScrollToTop = true
    init {
        resetQuery()
    }
    fun resetQuery(){
        currentPage = 1
        totalPage = 1
        currentKey = keywords.random()
        isNewQuery = true
        needToScrollToTop = true
        fetchData()
    }
    fun fetchData(){
        if (isLoading) return
        if (currentPage>totalPage) {
            _dataStatusLive.value = DATA_STATUS_NO_MORE
            return
        }
        isLoading = true
        val stringRequest = StringRequest(
            Request.Method.GET,
            getUrl(),
            {
                with(Gson().fromJson(it,Pixabay::class.java)){
                    totalPage = ceil(totalHits.toDouble()/perPage).toInt()
                    if (isNewQuery){
                        _photoListLive.value = hits.toList()
                    }else{
                        _photoListLive.value = arrayListOf(_photoListLive.value!!,hits.toList()).flatten()
                    }
                }
                _dataStatusLive.value = DATA_STATUS_CAN_LOAD_MORE
                isLoading = false
                isNewQuery = false
                currentPage++
            },
            {
                isLoading = false
                _dataStatusLive.value = DATA_STATUS_NETWORK_ERROR
            }
        )
        VolleySingleton.getInstance(getApplication()).requestQueue.add(stringRequest)
    }
    private fun getUrl() = "https://pixabay.com/api/?key=20130160-5126d62d4db8fcd58173c7f09&q=$currentKey&per_page=$perPage&page=$currentPage"


}